<!-- 弹窗用户 -->

<template>
  <div>
    <BtnText v-if="one.assignUserId" @click="toOpen">
      {{ one.assignUserName }}{{ one.assignUserPhone ? `(${one.assignUserPhone})` : '' }}
    </BtnText>

    <span v-if="!isShow">
      <BtnText v-if="one.assignUserId" title="清除" class="marginRight" @click="toClear" />
      <BtnPrimary v-else title="选择" @click="toOpen" />
    </span>

    <EasyModal v-model="easyVisible" title="选择指派人" @submit="toSubmit" @close="toReset">
      <ChooseUserOne :obj="obj" bind="user" :organId="obj.user.organId[0]" repair />
    </EasyModal>
  </div>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

const EMPTY = { uaaUserId: '', name: '', phone: '', organId: [] }

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'ModalUser',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb()],

  /**
   * 状态数据 (函数返回对象)
   */
  data() {
    return {
      easyVisible: false, // 弹窗是否可见
      obj: { user: CORE.clone(EMPTY) }, // 数据对象
    }
  },

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听弹窗可见
     */
    visible: {
      immediate: true,
      handler(visible) {
        visible && this.toReset()
      },
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 弹窗
     */
    toOpen() {
      this.easyVisible = true
    },

    /**
     * 提交
     */
    toSubmit() {
      const { obj, one } = this

      const organId = obj.user.organId[0] || ''
      const organ = CORE.ORGAN.MAP[organId] || {}

      CORE.merge(one, {
        assignUserId: obj.user.uaaUserId,
        assignUserName: obj.user.name,
        assignUserPhone: obj.user.phone,
        organizationId: organ.id,
        organizationName: organ.name,
      })
      this.easyVisible = false
    },

    /**
     * 重置
     */
    toReset() {
      const { assignUserId: uaaUserId, assignUserName: name, assignUserPhone: phone, organizationId } = this.one
      const organId = organizationId ? [organizationId] : []
      this.obj = { user: { uaaUserId, name, phone, organId } }
    },

    /**
     * 清除
     */
    toClear() {
      CORE.merge(this.one, {
        assignUserId: '',
        assignUserName: '',
        assignUserPhone: '',
        organizationId: '',
        organizationName: '',
      })
      this.obj = { user: CORE.clone(EMPTY) }
    },
  },
}
</script>
